package flc.ast.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.AlbumActivity;
import flc.ast.adapter.SelPictureAdapter;
import flc.ast.bean.MyAlbumBean;
import flc.ast.databinding.DialogAddPicBinding;
import flc.ast.utils.AppSpUtils;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.base.BaseSmartDialog;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class AddPicDialog extends BaseSmartDialog<DialogAddPicBinding> implements View.OnClickListener {

    private SelPictureAdapter pictureAdapter;
    private List<String> listSel = new ArrayList<>();

    public AddPicDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_add_pic;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvDialogAddPicAdd.setOnClickListener(this);
        mDataBinding.ivDialogAddPicCancel.setOnClickListener(this);

        mDataBinding.rvPictureList.setLayoutManager(new GridLayoutManager(getContext(), 3));
        pictureAdapter = new SelPictureAdapter();
        pictureAdapter.setManage(true);
        mDataBinding.rvPictureList.setAdapter(pictureAdapter);
        pictureAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (pictureAdapter.getItem(position).isChecked()) {
                    pictureAdapter.getItem(position).setChecked(false);
                    listSel.remove(pictureAdapter.getItem(position).getPath());
                } else {
                    pictureAdapter.getItem(position).setChecked(true);
                    listSel.add(pictureAdapter.getItem(position).getPath());
                }
                pictureAdapter.notifyItemChanged(position);
                mDataBinding.tvDialogAddPicAdd.setText(getContext().getString(R.string.add_num, listSel.size()));
            }
        });

        mDataBinding.tvDialogAddPicAdd.setText(getContext().getString(R.string.add_num, 0));

        getPermission();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvDialogAddPicAdd:
                List<MyAlbumBean> listSp = AppSpUtils.getAlbumList();
                if (listSp != null && listSp.size() > 0) {
                    for (MyAlbumBean bean : listSp) {
                        if (bean.getId().equals(AlbumActivity.albumId)) {
                            if (bean.getListData() != null && bean.getListData().size() > 0) {
                                bean.getListData().addAll(listSel);
                            } else {
                                bean.setListData(listSel);
                            }
                            break;
                        }
                    }
                    AppSpUtils.saveAlbumList(listSp);
                }
                listener.clickAdd();
                dismiss();
                break;
            case R.id.ivDialogAddPicCancel:
                dismiss();
                break;
        }
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO).reqPermissionDesc(getContext().getString(R.string.get_picture_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                getData();
            }

            @Override
            public void onDenied(boolean needGoSettings) {
                mDataBinding.rvPictureList.setVisibility(View.GONE);
            }
        }).request();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(getContext(), PhotoConfig.Mode.PHOTO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    pictureAdapter.setList(selectMediaEntities);
                    mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
                    mDataBinding.tvPictureNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvPictureList.setVisibility(View.GONE);
                    mDataBinding.tvPictureNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }


    private Listener listener;

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public interface Listener {
        void clickAdd();
    }
}
